<template>
  <div class="nav-menu">
    <!-- logo区域 -->
    <div class="logo">
      <img class="img" src="~@/assets/img/logo.svg" alt="" />
      <span v-if="!collapse">Bubble</span>
    </div>

    <!-- 菜单区域 -->
    <el-menu
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#b7bdc3"
      class="el-menu-vertical"
      :unique-opened="true"
      :collapse="collapse"
      :collapse-transition="false"
      :default-active="defaultValue"
    >
      <template v-for="subItem in userMenus" :key="subItem.id">
        <!-- 二级菜单 -->
        <template v-if="subItem.type === 1">
          <el-submenu :index="subItem.id + ''">
            <template #title>
              <i v-if="subItem.icon" :class="subItem.icon"></i>
              <span>{{ subItem.name }}</span>
            </template>
            <template v-for="item in subItem.children" :key="item.id">
              <template v-if="item.type === 2">
                <el-menu-item
                  :index="item.url"
                  @click="handleMenuItemClick(item)"
                  >{{ item.name }}</el-menu-item
                >
              </template>
            </template>
          </el-submenu>
        </template>
        <!-- 一级菜单 -->
        <template v-else-if="subItem.type === 2">
          <el-menu-item :index="subItem.id + ''">
            <i v-if="subItem.icon" :class="subItem.icon"></i>
            <span>{{ subItem.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { useVuexState } from '@/store'
import { mapPathToMenu } from '@/utils/map-menus'
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

interface Props {
  collapse?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  collapse: false
})

const store = useVuexState()
const userMenus = computed(() => store.state.login.userMenus)
const router = useRouter()
const route = useRoute()
const currentPath = route.path

const menu = mapPathToMenu(userMenus.value, currentPath)
const defaultValue = ref(menu.id + '')
const handleMenuItemClick = (item: any) => {
  router.push({
    path: item.url ?? '/not-found'
  })
}
onMounted(() => {
  console.log(userMenus.value)
  console.log(menu)
})
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  .logo {
    height: 30px;
    padding: 10px 10px 20px 7px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .img {
      height: 100%;
      margin: 0 10px;
    }
    span {
      font-size: large;
      color: #fff;
    }
  }
  .el-menu {
    border-right: none;
  }

  // 目录
  .el-submenu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
  }

  ::v-deep .el-submenu__title {
    background-color: #001529 !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #ffd42c !important;
    background-color: #a058fe !important;
  }
  .el-menu-vertical:not(.el-menu--collapse) {
    width: 100%;
    height: calc(100% - 60px);
  }
}
</style>
